package tsli.acm.ctrl;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tsli.acm.dao.PersistencyDAO;
import tsli.acm.dao.SFCCommissionPositiveDAO;
import tsli.acm.database.Database;
import tsli.acm.model.SFCCommissionPositive;

/**
 * Servlet implementation class ACM1204Srvl
 */
public class ACM1204Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ACM1204Srvl() {
        super();
    }
    
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String vaClosYm = "2014";
//		String vaMounth = request.getParameter( "inputMonth");
//		vaClosYm+= vaMounth;
//		Database db = new Database();
//		DecimalFormat voDecimalFormat = new DecimalFormat("#,###,###.##");
//		DecimalFormat voDecimalFormat2 = new DecimalFormat("#,###,###");
//		String vaAcProducerAmount ="", vaAcProducerMoney ="", vaAcProducerData ="" , IsEmptyGraderData = "no" , IsEmptyProductionData = "no", vaShowPageDetail = "yes";
//		double vnProductionPart = 0.0 , vnOrganizationPart = 0.0, vnTotalOE = 0.0, vnProductPartPC = 0.0, vnARYP = 0.0, vnProductPartPersistency = 0.0;
//		List<SFCCommissionPositive> voSFCCommissionPositiveLIst = new ArrayList<SFCCommissionPositive>();
//		try {
//			SFCCommissionPositiveDAO voSFCCommissionPositiveDAO = new SFCCommissionPositiveDAO(db);
//			PersistencyDAO voPersistencyDAO = new PersistencyDAO(db);
//			String vaTotalGrader = voSFCCommissionPositiveDAO.findTotalGrader(vaClosYm);			
//				List<Map<String, Object>> voGraderList = voSFCCommissionPositiveDAO.findGraderList(vaClosYm);
//				vaAcProducerData = voSFCCommissionPositiveDAO.findActiveProducer(vaClosYm);
//				String voProducerDataSplit[] = vaAcProducerData.split("/");
//				vaAcProducerMoney  = voProducerDataSplit[0];
//				vaAcProducerAmount = voProducerDataSplit[1];
//				vnOrganizationPart = Double.parseDouble( vaTotalGrader) + Double.parseDouble( vaAcProducerMoney);
//				String[] voDataProductionPart = voPersistencyDAO.getProductionPartForOE(vaClosYm).split("/");
//				vnProductionPart = Double.parseDouble(voDataProductionPart[0]);
//				vnProductPartPC =  Double.parseDouble(voDataProductionPart[1]);
//				vnARYP =  Double.parseDouble(voDataProductionPart[2]);
//				vnProductPartPersistency =  Double.parseDouble(voDataProductionPart[3]);
//				vnTotalOE = vnOrganizationPart + vnProductionPart;
//				
//				for ( int i = 0; i < voGraderList.size() ;i++){
//					Map<String, Object> voObject = voGraderList.get(i);
//					SFCCommissionPositive voSFCCommissionPositive = new SFCCommissionPositive();
//					// set GRADER
//					voSFCCommissionPositive.setVaApe( voObject.get("GRADER").toString());
//					// set PERSON_PER_GRADER
//					voSFCCommissionPositive.setVaContYmd( voObject.get("PERSON_PER_GRADER").toString());
//					int vnMoneyPerPerson = Integer.parseInt( voObject.get("MONEY_PER_PERSON").toString());
//					int vnPersonPerGrader = Integer.parseInt( voObject.get("PERSON_PER_GRADER").toString());
//					int vnMoney = vnMoneyPerPerson / vnPersonPerGrader;
//					// set MONEY_PER_PERSON
//					voSFCCommissionPositive.setVaBatchType( String.valueOf( voDecimalFormat2.format(vnMoney)));
//					voSFCCommissionPositiveLIst.add(voSFCCommissionPositive);
//				}
//			if ( voGraderList.size() == 0){
//				IsEmptyGraderData = "yes";
//			}
//			if ( voDataProductionPart.length == 0){
//				IsEmptyProductionData = "yes";
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			db.close();	
//		}	
//		request.setAttribute("vaAcProducer", vaAcProducerAmount+" คน "+" ("+vaAcProducerMoney+")");
//		request.setAttribute("vnOrganizationPart", vnOrganizationPart);
//		request.setAttribute("vnTotalOE", voDecimalFormat.format(vnTotalOE));
//		request.setAttribute("voGraderList", voSFCCommissionPositiveLIst);
//		request.setAttribute("vnProductionPart", vnProductionPart);
//		request.setAttribute("vnProductPartPC", vnProductPartPC);
//		request.setAttribute("vnARYP", vnARYP);
//		request.setAttribute("IsEmptyGraderData", IsEmptyGraderData);
//		request.setAttribute("IsEmptyProductionData", IsEmptyProductionData);
//		request.setAttribute("vnProductPartPersistency", vnProductPartPersistency);
//		request.setAttribute("vaShowPageDetail", vaShowPageDetail);
////		}	
//		// forward back to jsp
//    	RequestDispatcher rd = request.getRequestDispatcher("ACM1204.jsp");
//    	rd.forward(request, response);
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

}
